﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.ComponentModel;
using EcsSiteDSTableAdapters;

namespace SiteECSWeb
{

    [DataObject]
    public class TenderBLL
    {
        private TendersTableAdapter _TendersAdapter = null;

        protected TendersTableAdapter TendersAdapter
        {
            get
            {
                if (_TendersAdapter == null)
                {
                    _TendersAdapter = new TendersTableAdapter();
                }
                return _TendersAdapter;
            }
        }

        #region SelectTenderData

        //to get all Tenders Table data //
        [DataObjectMethodAttribute(DataObjectMethodType.Select, true)]
        public EcsSiteDS.TendersDataTable GetAllTenders()
        {
            return TendersAdapter.GetTendersRowRankData();
        }

        //[DataObjectMethodAttribute(DataObjectMethodType.Select, false)]
        //public EcsSiteDS.TendersDataTable GetAllTendersCountries()
        //{
        //    return TendersAdapter.GetTendersCountriesData();
        //}

        public int GetTotalNumberOfTenders()
        {
            Nullable<int> totalNull =  (Nullable<int>)TendersAdapter.TotalNumberOfTenders();
            return totalNull.GetValueOrDefault();
        }
        public int GetTotalNumberOfTenders(string FilterStr)
        {
            if (string.IsNullOrEmpty(FilterStr))
            {
                return GetTotalNumberOfTenders();
            }
            else
            {
                Nullable<int> totalNull = (Nullable<int>)TendersAdapter.
                TotalNumberOfTendersByFilterExp(FilterStr);
                return totalNull.GetValueOrDefault();
            }
        }

        // Get all the tenders Data with Wcompany Name//
        //[DataObjectMethodAttribute(DataObjectMethodType.Select, false)]
        //public EcsSiteDS.TendersDataTable GetTendersWithWcompany()
        //{
        //    return TendersAdapter.GetTenderDataWithWCompany();
        //}

        // To Get all Top 5 Tenders Data for the first Page//
        [DataObjectMethodAttribute(DataObjectMethodType.Select, false)]
        public EcsSiteDS.TendersDataTable GetTopTendersDataRank()
        {
            return TendersAdapter.GetTendersDataTopRank();
        }


        // Get Tender by Rank ID //
        [DataObjectMethodAttribute(DataObjectMethodType.Select, false)]
        public EcsSiteDS.TendersDataTable GetTendersDataByRank(int RowRank)
        {
            return TendersAdapter.GetTendersDataByRowRank(RowRank);
        }
        // Get Tender by Rank ID //
        [DataObjectMethodAttribute(DataObjectMethodType.Select, false)]
        public EcsSiteDS.TendersDataTable GetTendersByTenderID(int TenderID)
        {
            return TendersAdapter.GetTendersDataByTenderID(TenderID);
        }
        [DataObjectMethodAttribute(DataObjectMethodType.Select, false)]
        public EcsSiteDS.TendersDataTable 
            GetTendersByTenderNmArFrstLetter(string Tend_name , int TopCount)
        {
            return TendersAdapter.
            GetTendersDataByTenderNmArFrstLetter(Tend_name , TopCount);
        }
        [DataObjectMethodAttribute(DataObjectMethodType.Select, false)]
        public EcsSiteDS.TendersDataTable 
            GetTendersByCityIDYearMonth(int cityId, int year, int month)
        {
            return TendersAdapter.GetTendersDataByCityIdYearMonth(cityId, year, month);
        }
        //Get all tenders by city_id //
        [DataObjectMethodAttribute(DataObjectMethodType.Select, false)]
        public EcsSiteDS.TendersDataTable GetAllTendersByCityId(string City_nm)
        {
            return TendersAdapter.GetTendersDataByCityNmEn(City_nm);

            //if (City_nm )
            //{
            //    return TendersAdapter.GetAllTenderData();
            //}
            //else
            //{
            //    return TendersAdapter.GetTenderDataByCityID(City_nm);
            //}
        }

        // Get Count tender By Country //
        //[DataObjectMethodAttribute(DataObjectMethodType.Select, false)]
        //public EcsSiteDS.TendersDataTable GetCountTenderWithCountry(string Cuntry_un2)
        //{
        //    if (Cuntry_un2 == null)
        //    {
        //        return TendersAdapter.GetTendersRowRankData();
        //    }
        //    else
        //    {
        //        return TendersAdapter.GetCountTenderDataByCountry(Cuntry_un2);
        //    }
        //}


        ////Get Tender Count With City//
        //[DataObjectMethodAttribute(DataObjectMethodType.Select, false)]
        //public EcsSiteDS.TendersDataTable GetCountTenderWithCity()
        //{
        //    return TendersAdapter.GetCountTenderWithCity();
        //}

        ////Get Tender Count Wtih Country//
        //[DataObjectMethodAttribute(DataObjectMethodType.Select, false)]
        //public EcsSiteDS.TendersDataTable GetCountTenderWithCountry()
        //{
        //    return TendersAdapter.GetCountTenderDataWithCountry();
        //}

        ////Get Tender Count With EcsInternal//

        //[DataObjectMethodAttribute(DataObjectMethodType.Select, false)]
        //public EcsSiteDS.TendersDataTable GetCountTenderWithEcsinternal()
        //{
        //    return TendersAdapter.GetCountTenderDataWithEcsInternal();
        //}

        ////Get All tenders With City name//
        //[DataObjectMethodAttribute(DataObjectMethodType.Select, false)]
        //public EcsSiteDS.TendersDataTable GetTendersWithOfficeName()
        //{
        //    return TendersAdapter.GetTenderDataWithCity();
        //}

        //get all tenders by countrynameEN with stored proceduers//
        [DataObjectMethodAttribute(DataObjectMethodType.Select, false)]
        public EcsSiteDS.TendersDataTable GetSP_AllTendersByCountryName(string CountryNameEN)
        {
            return TendersAdapter.GetTendersDataByCountryNmEn(CountryNameEN);
            //if (CountryNameEN == null)
            //{
            //    return TendersAdapter.GetAllTenderData();
            //}
            //else
            //{
            //    return TendersAdapter.GetAllTenderDataByCountryNameEn(CountryNameEN);
            //}

        }

        
        [DataObjectMethodAttribute(DataObjectMethodType.Select, false)]
        public EcsSiteDS.TendersDataTable GetTendersAsGridPagesAndFilterExpression(int startRowIndex, 
            int maximumRows, string sortExpression)
        {
            return TendersAdapter.GetTendersDataByGridPages(startRowIndex, maximumRows, sortExpression);
            //EcsSiteDS.TendersDataTable tenders = GetAllTenders();
            //PagedDataSource pagedData = new PagedDataSource();
            //pagedData.DataSource = tenders.Rows;
            //pagedData.AllowPaging = true;
            //pagedData.CurrentPageIndex = pageIndex;
            //pagedData.PageSize = pageSize;
            //return pagedData;
        }
       
        [DataObjectMethodAttribute(DataObjectMethodType.Select, false)]
        public EcsSiteDS.TendersDataTable GetTendersAsGridPagesAndFilterExpression(int startRowIndex, int maximumRows, 
            string FilterStr, string sortExpression)
        {
            if (string.IsNullOrEmpty(FilterStr))
            {
                return
                    GetTendersAsGridPagesAndFilterExpression(startRowIndex, maximumRows, sortExpression);
            }
            else
            {
                return TendersAdapter.GetTendersDataByGridPagesFilterExp
                    (startRowIndex, maximumRows, FilterStr, sortExpression);
            }
        }


        #endregion

        #region InserTenderData
        [DataObjectMethodAttribute(DataObjectMethodType.Insert, true)]
        public int AddTender(int Wcomp_id, string Tend_name, string Tend_codeNum,
            string Tend_descrp, DateTime Tend_stdate, DateTime Tend_eddate,
            string Tend_specif, string Tend_howtoreg, string Tend_notes, int? ProdCat_Id,
            string Tend_registerBook, string Tend_path, 
            bool Formemberonly, bool? Publishing)
        {
            EcsSiteDS.TendersDataTable TenderTable = new EcsSiteDS.TendersDataTable();
            EcsSiteDS.TendersRow TenderRow = TenderTable.NewTendersRow();

            if (string.IsNullOrEmpty(Tend_codeNum)) TenderRow.SetTend_codeNumNull(); else TenderRow.Tend_codeNum = Tend_codeNum;
            if (string.IsNullOrEmpty(Tend_specif)) TenderRow.SetTend_specifNull(); else TenderRow.Tend_specif = Tend_specif;
            if (string.IsNullOrEmpty(Tend_howtoreg)) TenderRow.SetTend_howtoregNull(); else TenderRow.Tend_howtoreg = Tend_howtoreg;
            if (string.IsNullOrEmpty(Tend_notes)) TenderRow.SetTend_notesNull(); else TenderRow.Tend_notes = Tend_notes;
            if (string.IsNullOrEmpty(Tend_registerBook)) TenderRow.SetTend_registerBookNull(); else TenderRow.Tend_registerBook = Tend_registerBook;
            if (string.IsNullOrEmpty(Tend_path)) TenderRow.SetTend_pathNull(); else TenderRow.Tend_path = Tend_path;
            if (Publishing == null) TenderRow.SetPublishingNull(); else TenderRow.Publishing = Publishing.Value;
            if (ProdCat_Id == null) TenderRow.SetProdCat_IdNull(); else TenderRow.ProdCat_Id = ProdCat_Id.Value;

            TenderRow.Wcomp_id = Wcomp_id;
            TenderRow.Tend_name = Tend_name;
            TenderRow.Tend_descrp = Tend_descrp;
            TenderRow.Tend_stdate = Tend_stdate;
            TenderRow.Tend_eddate = Tend_eddate;
            TenderRow.Formemberonly = Formemberonly;

            TenderTable.AddTendersRow(TenderRow);
            
            int rowsAffected = TendersAdapter.Update(TenderTable);
            
            return (rowsAffected == 1) ? TenderRow.Tend_id : -1;
        }

        #endregion

        #region UpdatenderData

        [DataObjectMethodAttribute(DataObjectMethodType.Update, true)]

        public bool updatTender(int Wcomp_id, string Tend_name, string Tend_codeNum,
            string Tend_descrp, DateTime Tend_stdate, DateTime Tend_eddate,
            string Tend_specif,  string Tend_howtoreg, string Tend_notes, int? ProdCat_Id,
            string Tend_registerBook, string Tend_path, 
            bool Formemberonly, bool? Publishing, int Tend_id)
        {
            EcsSiteDS.TendersDataTable TenderTable = TendersAdapter.GetTendersDataByTenderID(Tend_id);
            if (TenderTable.Count == 0)
            {
                return false;
            }
            EcsSiteDS.TendersRow TenderRow = TenderTable[0];

            if (string.IsNullOrEmpty(Tend_codeNum )) TenderRow.SetTend_codeNumNull(); else TenderRow.Tend_codeNum = Tend_codeNum;
            if (string.IsNullOrEmpty(Tend_specif )) TenderRow.SetTend_specifNull(); else TenderRow.Tend_specif = Tend_specif;
            if (string.IsNullOrEmpty(Tend_howtoreg )) TenderRow.SetTend_howtoregNull(); else TenderRow.Tend_howtoreg = Tend_howtoreg;
            if (string.IsNullOrEmpty(Tend_notes )) TenderRow.SetTend_notesNull(); else TenderRow.Tend_notes = Tend_notes;
            if (string.IsNullOrEmpty(Tend_registerBook )) TenderRow.SetTend_registerBookNull(); else TenderRow.Tend_registerBook = Tend_registerBook;
            if (string.IsNullOrEmpty(Tend_path )) TenderRow.SetTend_pathNull(); else TenderRow.Tend_path = Tend_path;
            if (Publishing == null) TenderRow.SetPublishingNull(); else TenderRow.Publishing = Publishing.Value;
            if (ProdCat_Id == null) TenderRow.SetProdCat_IdNull(); else TenderRow.ProdCat_Id = ProdCat_Id.Value;

            TenderRow.Wcomp_id = Wcomp_id;
            TenderRow.Tend_name = Tend_name;
            TenderRow.Tend_descrp = Tend_descrp;
            TenderRow.Tend_stdate = Tend_stdate;
            TenderRow.Tend_eddate = Tend_eddate;
            TenderRow.Formemberonly = Formemberonly;
            //  TenderRow.Tend_id = Tend_id;

            int roweffected = TendersAdapter.Update(TenderRow);
            return roweffected == 1;
        }

        #endregion

        #region DeleleTenderData
        [DataObjectMethodAttribute(DataObjectMethodType.Delete, true)]
        public bool DeleteTednder(int Tend_id)
        {
            EcsSiteDS.TendersDataTable TenderTable = TendersAdapter.GetTendersDataByTenderID(Tend_id);
            if (TenderTable.Count == 0)
            {
                return false;
            }
            EcsSiteDS.TendersRow TenderRow = TenderTable[0];
            if (!TenderRow.IsTend_pathNull())
            {
                System.IO.File.Delete(HttpContext.Current.Server.MapPath(Globals.Settings.UploadInfo.RootAttachFolder
                           + TenderRow.Tend_path));
            }
            
            int rowsAffected = TendersAdapter.Delete(Tend_id);
            return rowsAffected == 1;
        }

        #endregion
    }
}